package com.sky.controller.user;

import com.sky.dto.UserLoginDTO;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "用户相关接口")
@Slf4j
@RestController
@RequestMapping("/user/user")
public class UserLoginController {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result<UserLoginVO> userLogin(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户的编码为，{}",userLoginDTO.getCode());
        //微信登录
        UserLoginVO userLoginVO = userService.userLogin(userLoginDTO);

        //微信登录成功之后下发令牌
        Map<String, Object> claims = new HashMap();
        claims.put("userId",userLoginVO.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        userLoginVO.setToken(token);
        return Result.success(userLoginVO);
        }

}
